﻿using GKQC.Common;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GKQC.DbHelper.Dao
{
    public class AllDbContext
    {
        string s1 = "server={0};uid={1};pwd={2};database={3}";
        string s2 = "User Id={0}; Password = {1}; Data Source = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = {2})(PORT = {3})))(CONNECT_DATA = (SERVICE_NAME = {4})))";
        string s3 = "server={0};uid={1};pwd={2};database={3}";
        string con_sqlite = "Data Source={0};Version=3;Pooling=true;FailIfMissing=false";
        public AllDbContext()
        {
            var es = EncryptSugar.GetInstance();
            CommonString commonString = Settings.GetDataObjectFile<CommonString>("CommonString.Dat");
            s1 = string.Format(s1
                , commonString.S1 + (string.IsNullOrWhiteSpace(commonString.S3) ? string.Empty : ("," + commonString.S3))
                , commonString.S4
                , (commonString.S5.Length>50 ? es.Decrypto(commonString.S5.Substring(0, commonString.S5.Length -50)) : commonString.S5)
                , commonString.S2);

            //s2 = string.Format(s2
            //    , commonString.S14
            //    , (commonString.S25.Length > 50 ? es.Decrypto(commonString.S25.Substring(0, commonString.S25.Length - 50)) : commonString.S25)
            //    , commonString.S21
            //    , string.IsNullOrWhiteSpace(commonString.S13) ? "1521" : commonString.S13
            //    , commonString.S13
            //    , commonString.S12);

            s2 = string.Format(s2
            , commonString.S14
            , (commonString.S15.Length > 50 ? es.Decrypto(commonString.S15.Substring(0, commonString.S15.Length - 50)) : commonString.S15)
            , commonString.S11
            , string.IsNullOrWhiteSpace(commonString.S13) ? "1521" : commonString.S13
            , commonString.S12);

            //s3 = string.Format(s3
            //    , commonString.S21 + (string.IsNullOrWhiteSpace(commonString.S23) ? string.Empty : ("," + commonString.S23))
            //    , commonString.S24
            //    , (commonString.S25.Length > 50 ? es.Decrypto(commonString.S25.Substring(0, commonString.S25.Length - 50)) : commonString.S25)
            //    , commonString.S22);

            if (string.IsNullOrEmpty(commonString.S1))
                con_sqlite = string.Format(con_sqlite, Settings.GetDataPath() + "\\lcrmesdata.db");
            else
                con_sqlite = string.Format(con_sqlite, commonString.S1);

            #region 新檢測系統 
            try
            {
                if (commonString.DbType == 4)
                    QcDb = new SqlSugarClient(new ConnectionConfig()
                    {
                        ConnectionString = con_sqlite,
                        DbType = DbType.Sqlite,
                        InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
                        IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了
                    }); 
                else
                QcDb = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = s1,
                    DbType = DbType.SqlServer,
                    InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
                    IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了

                });

                //todo:调式代码 用来打印SQL 
#if DEBUG
            QcDb.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(sql + "\r\n" +
                    QcDb.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                Console.WriteLine();
            };
#endif
            }
            catch (Exception ex)
            {
                Logger.LogError(ex);
            }

#endregion

#region ERP系統
            try
            {
                //Logger.LogMessage("orclStringRtue:\r\n" + s2);
                ErpDb = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = s2,//"User Id=scott; Password = Qq123456; Data Source = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521)))(CONNECT_DATA = (SERVICE_NAME = orcl)))",
                    DbType = DbType.Oracle,
                    InitKeyType = InitKeyType.Attribute,
                    IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了

                });

                //todo: 调式代码 用来打印SQL 
#if DEBUG
                ErpDb.Aop.OnLogExecuting = (sql, pars) =>
                {
                    Console.WriteLine(sql + "\r\n" +
                        ErpDb.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                    Console.WriteLine();
                };
#endif
            }
            catch(Exception ex)
            {
                Logger.LogError(ex);
            }

#endregion

#region 老的報表系統
            try
            {
                OldDb = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = s3,//"server=.;uid=sa;pwd=123456;database=rpdb", 
                    DbType = DbType.SqlServer,
                    InitKeyType = InitKeyType.Attribute,
                    IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了

                });
                //调式代码 用来打印SQL 
#if DEBUG
                OldDb.Aop.OnLogExecuting = (sql, pars) =>
                {
                    Console.WriteLine(sql + "\r\n" +
                        OldDb.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                    Console.WriteLine();
                };
#endif
            }
            catch(Exception ex)
            {
                Logger.LogError(ex);
            }
#endregion

        }

        /// <summary>
        /// 檢測系統DB
        /// </summary>
        public SqlSugarClient QcDb;//用来处理事务多表查询和复杂的操作
        /// <summary>
        /// ERP DB
        /// </summary>
        public SqlSugarClient ErpDb;//用来处理事务多表查询和复杂的操作
        /// <summary>
        /// 老的報表系統DB
        /// </summary>
        public SqlSugarClient OldDb;//用来处理事务多表查询和复杂的操作

        public DateTime? GetServerTime()
        {
            DateTime serverTime = DateTime.Now;
            var result = QcDb.GetDate();
            return serverTime;
        }
    
    }
}
